package cn.leedsoft.luma.common.utils;

import cn.leedsoft.luma.plugins.spring.SpringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.util.Assert;

/**
 * Created by leeds on 2016/6/22 15:29.
 */
public class EhcacheUtil {
    private static final Logger logger = LoggerFactory.getLogger(EhcacheUtil.class);

    public static void removeCache(String cacheName) {
        Assert.notNull(cacheName, "cache name can't be null!");

        EhCacheCacheManager ehCacheCacheManager = SpringHelper.getBean(EhCacheCacheManager.class);
        Cache cache = ehCacheCacheManager.getCache(cacheName);
        Assert.notNull(cache, String.format("ehcache [%s] does not exist!", cacheName));
        cache.clear();
        logger.info(String.format("ehcache [%s] has been removed successfully!", cacheName));
    }
}
